package com.lj.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.eduservice.entity.Teacher;
import com.lj.eduservice.entity.vo.TeacherQuery;
import com.lj.eduservice.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author luojing
 * @since 2021-04-14
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Api("教师管理接口")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/pageConditionTeacher/{current}/{size}")
    @ApiOperation("分页条件获取教师信息")
    public Page<Teacher> pageConditionTeacher(@PathVariable Long current, @PathVariable Long size, TeacherQuery teacherQuery) {
        Page<Teacher> teacherPage = new Page<>(current, size);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //根据条件拼接 wrapper
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        wrapper.orderByDesc("gmt_modified");
        teacherService.page(teacherPage, wrapper);
        return teacherPage;
    }

    @ApiOperation("查询出所有老师姓名和id的映射")
    @GetMapping("/getAllNameAndIdMapper")
    public Map<Object, Object> getAllNameAndIdMapper(){
        List<Map<String, Object>> maps = teacherService.listMaps(new QueryWrapper<Teacher>().select("id", "name"));
        Map<Object, Object> mapper= new HashMap<>();
        for (Map<String, Object> map : maps) {
            mapper.put(map.get("id"),map.get("name"));
        }
        return mapper;
    }

    @ApiOperation("删除指定id的教师")
    @DeleteMapping("/{id}")
    public Boolean delTeacherById(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        return teacherService.removeById(id);
    }

    @ApiOperation("新增/通过id修改教师")
    @PostMapping("/addOrupdateTeacher")
    public Boolean addOrupdateTeacher(@RequestBody Teacher teacher) throws InterruptedException {
        if (teacherService.saveOrUpdate(teacher)) {
            return true;
        }
        return false;
    }

    @ApiOperation("通过id查询教师")
    @GetMapping("/getTeacher/{id}")
    public Teacher getTeacher(@PathVariable String id) {
        return teacherService.getById(id);
    }

}

